package cn.iocoder.yudao.module.system.job.flow;

import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.system.controller.admin.messageInfo.vo.FeedBackVO;
import cn.iocoder.yudao.module.system.service.config.InfraConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目反馈
 */
@Component
@Slf4j
public class ProjectFeedbackJob implements JobHandler {

    @Resource
    private InfraConfigService infraConfigService;

    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        Integer size=0;
        List<FeedBackVO> list = infraConfigService.getEntryStudent();
        if(CollectionUtils.isNotEmpty(list)){
            size=list.size();
            try {
                List<FeedBackVO> newList =new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    FeedBackVO f = list.get(i);
                    newList.add(f);
                    if(i%100==0){
                        infraConfigService.saveProjectFeedBack(newList);
                        newList=new ArrayList<>();
                    }
                }
                if(CollectionUtils.isNotEmpty(newList)){
                    infraConfigService.saveProjectFeedBack(newList);
                }
                List<Long> ids = list.stream().map(FeedBackVO::getProjectId).distinct().collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(ids)){
                    infraConfigService.updateProjectFeedStatus(ids);
                }
            }catch (Exception e){
                log.error("ProjectFeedbackJob执行异常",e);
            }
        }
        return String.format("更新定时项目反馈个数 %s 个",size);
    }
}
